package com.moonshineproject.plugin.project.vo
{	
	import com.moonshineproject.plugin.core.sourcecontrol.ISourceControlProvider;
	
	import flash.filesystem.File;
	
	public class FileWrapper
	{
		protected var _file:File;
		protected var _isRoot:Boolean;
		protected var _defaultName:String;
		protected var _sourceController:ISourceControlProvider;
		
		public function FileWrapper(file:File, isRoot:Boolean=false)
		{
			_file = file;
			if (file && file.isDirectory) updateChildren();
			
			_isRoot = isRoot;
		}
		
		
		public function get sourceController():ISourceControlProvider
		{
			return _sourceController;	
		}
		public function set sourceController(v:ISourceControlProvider):void
		{
			if (_sourceController == v) return;
			
			_sourceController = v;
			
			if (!children) return;
			for (var i:int = 0; i < children.length; i++)
			{
				children[i].sourceController = v;
			}	
		}
		
		public function get file():File
		{
			return _file;
		}
		public function set file(v:File):void
		{
			_file = v;
			if (file && file.isDirectory) updateChildren();
		}
		
		public function get isRoot():Boolean
		{
			return _isRoot;
		}
		
		public function get name():String
		{
			if (file) return file.name;
			else return _defaultName;
		}
		
		public function get defaultName():String
		{
			return _defaultName;
		}
		public function set defaultName(v:String):void
		{
			_defaultName = v;
		}

		private var _children:Array;
		public function get children():Array
		{
			if (!_children) updateChildren();

			return _children;
		}
		
		public function get nativePath():String
		{
			if (!file) return null;
			return file.nativePath;
		}
		
		public function containsFile(file:File):Boolean
		{
			if (file.nativePath.indexOf(nativePath) == 0) return true;
			return false;
		}
		
		public function updateChildren():void
		{
			if (!file.isDirectory) return;
			
			var c:Array = file.getDirectoryListing();
			_children = [];
			var f:File;
			var fw:FileWrapper;
			for (var i:int = 0; i < c.length; i++)
			{
				f = c[i];
				if (!f.isHidden)
				{
					fw = new FileWrapper(f);
					fw.sourceController = _sourceController;
					_children.push(fw);
				}	
			}
		}

	}
}